/** 实现图标库的懒加载机制 */
import {ComponentType, LazyExoticComponent, Suspense, lazy} from 'react'

const defaultIconName = 'BarsOutlined'

const lazyIconMap: Record<string, LazyExoticComponent<ComponentType>> = {
    'HomeOutlined': lazy(() => import('@ant-design/icons/HomeOutlined.js').then((module) => ({default: module.default}))),
    'BarsOutlined': lazy(() => import('@ant-design/icons/BarsOutlined').then((module) => ({default: module.default}))),
    'UserOutlined': lazy(() => import('@ant-design/icons/UserOutlined').then((module) => ({default: module.default}))),
    'CalendarOutlined': lazy(() => import('@ant-design/icons/CalendarOutlined').then((module) => ({default: module.default}))),
    'LineOutlined': lazy(() => import('@ant-design/icons/LineOutlined').then((module) => ({default: module.default}))),

    'LoginOutlined': lazy(() => import('@ant-design/icons/LoginOutlined').then((module) => ({default: module.default}))),
    'LogoutOutlined': lazy(() => import('@ant-design/icons/LogoutOutlined').then((module) => ({default: module.default}))),

    'LoadingOutlined': lazy(() => import('@ant-design/icons/LoadingOutlined').then((module) => ({default: module.default}))),
    'ArrowRightOutlined': lazy(() => import('@ant-design/icons/ArrowRightOutlined').then((module) => ({default: module.default}))),
    'RightCircleOutlined': lazy(() => import('@ant-design/icons/RightCircleOutlined').then((module) => ({default: module.default}))),
    'DownCircleOutlined': lazy(() => import('@ant-design/icons/DownCircleOutlined').then((module) => ({default: module.default}))),
    'DownloadOutlined': lazy(() => import('@ant-design/icons/DownloadOutlined').then((module) => ({default: module.default}))),
    'CheckOutlined': lazy(() => import('@ant-design/icons/CheckOutlined').then((module) => ({default: module.default}))),
    'CloseOutlined': lazy(() => import('@ant-design/icons/CloseOutlined').then((module) => ({default: module.default}))),
    'CloseCircleOutlined': lazy(() => import('@ant-design/icons/CloseCircleOutlined').then((module) => ({default: module.default}))),
    'UndoOutlined': lazy(() => import('@ant-design/icons/UndoOutlined').then((module) => ({default: module.default}))),
    'RedoOutlined': lazy(() => import('@ant-design/icons/RedoOutlined').then((module) => ({default: module.default}))),
    'CheckCircleOutlined': lazy(() => import('@ant-design/icons/CheckCircleOutlined').then((module) => ({default: module.default}))),
    'ClockCircleOutlined': lazy(() => import('@ant-design/icons/ClockCircleOutlined').then((module) => ({default: module.default}))),
    'StopOutlined': lazy(() => import('@ant-design/icons/StopOutlined').then((module) => ({default: module.default}))),

    'QuestionCircleOutlined': lazy(() => import('@ant-design/icons/QuestionCircleOutlined').then((module) => ({default: module.default}))),

    'ExclamationCircleOutlined': lazy(() => import('@ant-design/icons/ExclamationCircleOutlined').then((module) => ({default: module.default}))),

    'SearchOutlined': lazy(() => import('@ant-design/icons/SearchOutlined').then((module) => ({default: module.default}))),
    'ClearOutlined': lazy(() => import('@ant-design/icons/ClearOutlined').then((module) => ({default: module.default}))),

    'PicCenterOutlined': lazy(() => import('@ant-design/icons/PicCenterOutlined').then((module) => ({default: module.default}))),
    'PlusOutlined': lazy(() => import('@ant-design/icons/PlusOutlined').then((module) => ({default: module.default}))),
    'RetweetOutlined': lazy(() => import('@ant-design/icons/RetweetOutlined').then((module) => ({default: module.default}))),
    'ShrinkOutlined': lazy(() => import('@ant-design/icons/ShrinkOutlined').then((module) => ({default: module.default}))),
    'EditOutlined': lazy(() => import('@ant-design/icons/EditOutlined').then((module) => ({default: module.default}))),
    'ExportOutlined': lazy(() => import('@ant-design/icons/ExportOutlined').then((module) => ({default: module.default}))),
    'LinkOutlined': lazy(() => import('@ant-design/icons/LinkOutlined').then((module) => ({default: module.default}))),
    'FileImageOutlined': lazy(() => import('@ant-design/icons/FileImageOutlined').then((module) => ({default: module.default}))),
}

/**
 * Description: 图表的按需加载.
 * @author <a href="mailto:scorpio.caplike@gmail.com">LiKe</a> @ 2025-02-23 14:44:32
 */
const LazyIcon = ({iconName}: { iconName?: string }) => {
    let theIconName = iconName ? iconName : defaultIconName;
    theIconName = theIconName.endsWith('Outlined') ? theIconName : theIconName + 'Outlined'
    const IconComponent = lazyIconMap[theIconName]

    return (
        <Suspense fallback={<></>}>
            <IconComponent/>
        </Suspense>
    )
}

export default LazyIcon
